package registnet.records;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;

import registnet.records.CourseTaken;

public class Form5 {

	private Student student;
	private int semester;
	private int year;
	private HashMap<String, ICourse> courses = new HashMap<String, ICourse>();
	
	
	/**
	 * Default constructor
	 * 
	 * @param student
	 * @param semester
	 * @param year
	 */
	public Form5(Student student, int semester, int year){
		this.student = student;
		this.semester = semester;
		this.year = year;
	}
	
	
	/**
	 * Gets the student who owns the Form5
	 * 
	 * @return	The student who owns the Form5
	 */
	public Student getStudent(){
		return this.student;
	}
	
	
	/**
	 * Sets the student who owns the Form5
	 * 
	 * @param student - The student who owns the Form5
	 */
	public void setStudent(Student student){
		this.student = student;
	}
	
	
	/**
	 * Gets the semester when the Form5 was 'issued'
	 * 
	 * @return	The semester when the Form5 was 'issued'
	 */
	public int getSemester(){
		return this.semester;
	}
	
	
	/**
	 * Sets the semester when the Form5 was 'issued'
	 * 
	 * @param semester - The semester when the Form5 was 'issued'
	 */
	public void setSemester(int semester){
		this.semester = semester;
	}
	
	
	/**
	 * Gets the year when the Form5 was 'issued'
	 * 
	 * @return	The year when the Form5 was 'issued'
	 */
	public int getYear(){
		return this.year;
	}
	
	
	/**
	 * Sets the year when the Form5 was 'issued'
	 * 
	 * @param year - The year when the Form5 was 'issued'
	 */
	public void setYear(int year){
		this.year = year;
	}
	
	
	/**
	 * Gets the courses included in the Form5
	 * 
	 * @return	The courses included in the Form5
	 */
	public HashMap<String, ICourse> getCourses(){
		return this.courses;
	}
	
	
	/**
	 * Adds the course into the Form5
	 * 
	 * @param course - The course to add
	 */
	public void addCourse(ICourse course){
		this.courses.put(course.getCourseCode(), course);
	}
	
	
	/**
	 * Gets the total number of units
	 * 
	 * @return The total number of units
	 */
	public int getTotalUnits(){
		int total = 0;
		Iterator<String> iter = this.courses.keySet().iterator();
		while(iter.hasNext()){
			String key = iter.next();
			CourseTaken course = (CourseTaken)this.courses.get(key);
			total += course.getUnit();
		}
		return total;
	}
	
	
	/**
	 * Gets the GWA of the student
	 * 
	 * @return The GWA of the student
	 */
	public double getGWA(){
		double GWA = 0;
		int totalUnits = 0;
		HashMap<String, ICourse> courses = this.getCourses();
		Iterator<String> iter = courses.keySet().iterator();
		DecimalFormat df = new DecimalFormat();
		df.setMinimumFractionDigits(4);
		while(iter.hasNext()){
			CourseTaken course = (CourseTaken)this.courses.get(iter.next());
			if (new Double(course.getReExam()) != new Double(0)){
				GWA += course.getGrade() * course.getUnit();
				totalUnits += course.getUnit();
			}
			else{
				GWA += course.getReExam() * course.getUnit();
				totalUnits += course.getUnit();
			}
		}
		return Double.parseDouble(df.format(GWA/totalUnits));
	}
	
}
